cmake_minimum_required(VERSION 3.5)
project(socketcan_interface)

# Set C++ standard before any targets
#if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 17)
  set(CMAKE_CXX_STANDARD_REQUIRED ON)
#endif()

# Find required packages
find_package(console_bridge_vendor REQUIRED)
find_package(Boost REQUIRED COMPONENTS
  chrono
  system
  thread
)

find_package(rclcpp REQUIRED)
find_package(Threads REQUIRED)
find_package(ament_cmake REQUIRED)
find_package(pluginlib REQUIRED)
find_package(class_loader REQUIRED)

# Include directories
include_directories(
  include
  ${Boost_INCLUDE_DIRS}
  ${console_bridge_INCLUDE_DIRS}
  ${pluginlib_INCLUDE_DIRS}
  ${class_loader_INCLUDE_DIRS}
)

# Main string library
add_library(${PROJECT_NAME}_string SHARED
    src/string.cpp
)
target_link_libraries(${PROJECT_NAME}_string
    ${Boost_LIBRARIES}
    console_bridge::console_bridge
)

# Plugin library
add_library(${PROJECT_NAME}_plugin SHARED
  src/${PROJECT_NAME}_plugin.cpp
)

target_link_libraries(${PROJECT_NAME}_plugin
  ${PROJECT_NAME}_string
  ${Boost_LIBRARIES}
  pluginlib::pluginlib
  class_loader::class_loader
  console_bridge::console_bridge
)

# Socketcan dump executable
add_executable(socketcan_dump
  src/candump.cpp
)

target_link_libraries(socketcan_dump 
  ${PROJECT_NAME}_string
  ${Boost_LIBRARIES}
  console_bridge::console_bridge
  class_loader::class_loader
)

# Socketcan BCM executable
add_executable(socketcan_bcm
  src/canbcm.cpp
)
target_link_libraries(socketcan_bcm
  ${PROJECT_NAME}_string
  ${Boost_LIBRARIES}
  console_bridge::console_bridge
)

# Plugin description file
pluginlib_export_plugin_description_file(${PROJECT_NAME} socketcan_interface_plugin.xml)

# Export and install
ament_export_include_directories(include)
ament_export_libraries(${PROJECT_NAME}_string ${PROJECT_NAME}_plugin)
ament_export_dependencies(
  Boost
  console_bridge_vendor
  rclcpp
  pluginlib
  class_loader
)

install(
  TARGETS
    socketcan_bcm
    socketcan_dump
    ${PROJECT_NAME}_plugin
    ${PROJECT_NAME}_string
  EXPORT export_${PROJECT_NAME}
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION lib/${PROJECT_NAME}
)

install(
  DIRECTORY
    include/${PROJECT_NAME}/
  DESTINATION include/${PROJECT_NAME}
  FILES_MATCHING PATTERN "*.h"
)

install(
  FILES
    ${PROJECT_NAME}_plugin.xml
  DESTINATION share/${PROJECT_NAME}
)

ament_package()